import random
import string

from django.db import models

# Create your models here.

class UserModel(models.Model):
    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    password = models.CharField(max_length=100, verbose_name='密码')
    nikename = models.CharField(max_length=32, verbose_name='用户昵称', null=True, blank=True)
    # 登录自动生成用户昵称
    def save(self, *args, **kwargs):
        if not self.nikename:
            self.nikename = self.generate_nikename()
        super().save(*args, **kwargs)

    @staticmethod
    def generate_nikename():
        first="".join(random.sample(string.ascii_letters, 3))
        middle = "_"
        backend=''.join(random.sample(string.ascii_letters + string.digits, 8))
        nikename = f"{first}{middle}{backend}"
        return nikename
    print(nikename)
    class Meta:
        db_table = 'app_user'
        verbose_name_plural = "用户登录表"
    def __str__(self):
        return self.mobile


class ThirdPartyLogin(models.Model):
    # 枚举类型，表示第三方登录的类型
    TYPES_CHOICES = [
        (0, 'Dingding'),
        (1, 'Weixin'),
    ]
    userid = models.ForeignKey('UserModel', on_delete=models.CASCADE,verbose_name='用户ID')
    types = models.CharField(max_length=10, choices=TYPES_CHOICES,verbose_name='第三方登录类型')
    # 第三方平台的用户ID
    uid = models.CharField(max_length=255,verbose_name='第三方平台用户ID')
    def __str__(self):
        return f"{self.types} login for user {self.userid}"